package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduComment;
import com.jf3q.common.domain.EduMember;
import com.jf3q.common.service.EduCommentService;
import com.jf3q.common.service.EduMemberService;
import com.jf3q.common.utils.JwtTokenUtil;
import com.jf3q.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Tag(name = "评论管理")
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;
    @Autowired
    private EduMemberService memberService;

    //根据课程id_分页查询课程评论的方法
    @GetMapping("/getCommentPage/{courseId}/{pageNum}/{pageSize}")
    @Operation(summary = "查询课程评论")
    public ResultVo getCommentPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String courseId) {
        LambdaQueryWrapper<EduComment> queryWrapper = new LambdaQueryWrapper<>();

        //判断课程id是否为空
        if (!StringUtils.isEmpty(courseId)) {
            queryWrapper.eq(EduComment::getCourseId, courseId);
        }
        //按最新排序
        queryWrapper.orderByDesc(EduComment::getEduCreate);
        //数据会被封装到page中
        Page<EduComment> page = commentService.page(new Page<>(pageNum, pageSize), queryWrapper);

        List<EduComment> commentList = page.getRecords();
        long current = page.getCurrent();//当前页
        long size = page.getSize();//一页记录数
        long total = page.getTotal();//总记录数
        long pages = page.getPages();//总页数
        boolean hasPrevious = page.hasPrevious();//是否有上页
        boolean hasNext = page.hasNext();//是否有下页

        HashMap<String, Object> map = new HashMap<>();
        map.put("current",current);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("list",commentList);


        return ResultVo.success("", map);
    }

    //添加评论
    @PostMapping("/addComment")
    @Operation(summary = "添加评论")
    public ResultVo addComment(HttpServletRequest request, @RequestBody EduComment eduComment){
        String token = request.getHeader("token");
        String memberId=JwtTokenUtil.getMemberIdByJwtToken(token);
        //判断用户是否登录
        if (StringUtils.isEmpty(memberId)) {
            return ResultVo.error("请先登录");
        }
        eduComment.setMemberId(memberId);
        //根据用户id查询用户信息
        EduMember eduMember = memberService.getById(memberId);

        eduComment.setAvatar(eduMember.getAvatar());
        eduComment.setNickname(eduMember.getNickname());

        //保存评论
        commentService.save(eduComment);
        return ResultVo.success("");
    }

    @DeleteMapping("/delComment/{id}")
    @Operation(summary = "删除评论")
    public ResultVo delComment(@PathVariable String id){
        commentService.removeById(id);
        return ResultVo.success("");
    }
}
